//
//  MessageController.m
//  Toyohu
//
//  Created by celia on 16/3/24.
//  Copyright (c) 2016年 celia. All rights reserved.
//

#import "MessageViewController.h"
#import "HMSegmentedControl.h"
#import <Masonry/Masonry.h>

#import "LeftTableViewCell.h"
#import "MiddleTableViewCell.h"
#import "RightTableViewCell.h"


#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
{
    UIScrollView *_bgScrollView;
    
    UITableView *_leftTableView;
    UITableView *_middleTableView;
    UITableView *_rightTableView;
    
    NSMutableArray *_leftArray;
    NSMutableArray *_middleArray;
    NSMutableArray *_rightArray;
    
    
}

@property (nonatomic, strong) HMSegmentedControl *segmentedControl;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
  
    
    
    [self initView];
    
    
}

- (void)initView{
    
    UILabel *navLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
    navLabel.textColor = [UIColor whiteColor];
    navLabel.backgroundColor = [UIColor clearColor];
    navLabel.textAlignment = NSTextAlignmentCenter;
    navLabel.text = @"消息中心";
    navLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
    self.navigationItem.titleView = navLabel;
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    self.segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"社区消息",@"专家消息",@"系统消息"]];
    
    self.segmentedControl.selectionIndicatorColor = [UIColor colorWithRed:106.0f/255.0f green:205.0f/255.0f blue:189.0f/255.0f alpha:1];
    self.segmentedControl.verticalDividerColor = [UIColor colorWithRed:106.0f/255.0f green:205.0f/255.0f blue:189.0f/255.0f alpha:1];
    self.segmentedControl.borderColor = [UIColor colorWithRed:106.0f/255.0f green:205.0f/255.0f blue:189.0f/255.0f alpha:1];
    
    self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    self.segmentedControl.frame = CGRectMake(self.view.bounds.size.width/2-100, 4, 200, 40);
    self.segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
    self.segmentedControl.selectionIndicatorHeight = 3.0f;
    self.segmentedControl.selectionIndicatorColor = [UIColor colorWithRed:106.0f/255.0f green:205.0f/255.0f blue:189.0f/255.0f alpha:1];
    self.segmentedControl.borderColor = [UIColor colorWithRed:106.0f/255.0f green:205.0f/255.0f blue:189.0f/255.0f alpha:1];
    self.segmentedControl.selectionIndicatorColor = [UIColor colorWithRed:106.0f/255.0f green:205.0f/255.0f blue:189.0f/255.0f alpha:1];
    self.segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    self.segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    [self.segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentedControl];
    self.segmentedControl.backgroundColor = [UIColor whiteColor];
    [self.segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        CGFloat height = self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height;
        make.top.equalTo(@(height));
        make.height.equalTo(@40);
    }];
    
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 66, WIDTH, HEIGHT - 66)];
    _bgScrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    _bgScrollView.bounces = NO;
    _bgScrollView.delegate = self;
    _bgScrollView.pagingEnabled = YES;
    _bgScrollView.showsHorizontalScrollIndicator = NO;
    _bgScrollView.showsVerticalScrollIndicator = NO;
    _bgScrollView.backgroundColor = RGBA(235, 234, 229, 1);
    [self.view addSubview:_bgScrollView];
    
    [_bgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(self.segmentedControl.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
    _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -30, WIDTH, _bgScrollView.frame.size.height - 57) style:UITableViewStyleGrouped];
    _leftTableView.delegate = self;
    _leftTableView.backgroundColor = RGBA(235, 234, 229, 1);
    _leftTableView.dataSource = self;
    _leftTableView.separatorColor = [UIColor clearColor];
    _leftTableView.tableHeaderView = [UIView new];
    _leftTableView.tableFooterView = [UIView new];
    _leftTableView.estimatedRowHeight = 130.0f;
    _leftTableView.rowHeight = UITableViewAutomaticDimension;
    [_leftTableView registerNib:[UINib nibWithNibName:@"LeftTableViewCell" bundle:nil] forCellReuseIdentifier:@"leftMessageID"];
    [_bgScrollView addSubview:_leftTableView];
    
    _middleTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH, -30, WIDTH, _bgScrollView.frame.size.height - 57) style:UITableViewStyleGrouped];
    _middleTableView.delegate = self;
    _middleTableView.backgroundColor = RGBA(235, 234, 229, 1);
    _middleTableView.dataSource = self;
    _middleTableView.tableHeaderView = [UIView new];
    _middleTableView.tableFooterView = [UIView new];
    _middleTableView.estimatedRowHeight = 130.0f;
    _middleTableView.rowHeight = UITableViewAutomaticDimension;
    [_middleTableView registerNib:[UINib nibWithNibName:@"MiddleTableViewCell" bundle:nil] forCellReuseIdentifier:@"middleMessageID"];
    [_bgScrollView addSubview:_middleTableView];
    _middleTableView.separatorColor = [UIColor clearColor];

    _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH * 2, -30, WIDTH, _bgScrollView.frame.size.height - 57) style:UITableViewStyleGrouped];
    _rightTableView.delegate = self;
    _rightTableView.backgroundColor = RGBA(235, 234, 229, 1);
    _rightTableView.dataSource = self;
    _rightTableView.tableHeaderView = [UIView new];
    _rightTableView.tableFooterView = [UIView new];
    _rightTableView.estimatedRowHeight = 130.0f;
    _rightTableView.rowHeight = UITableViewAutomaticDimension;
    [_rightTableView registerNib:[UINib nibWithNibName:@"RightTableViewCell" bundle:nil] forCellReuseIdentifier:@"rightMessageID"];
    [_bgScrollView addSubview:_rightTableView];
    _rightTableView.separatorColor = [UIColor clearColor];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    NSLog(@"scro = %f",scrollView.contentOffset.x);
    if (scrollView == _bgScrollView) {
        if (scrollView.contentOffset.x == 0) {
            _segmentedControl.selectedSegmentIndex = 0;
        } else if (scrollView.contentOffset.x == WIDTH ) {
            _segmentedControl.selectedSegmentIndex = 1;
        }else if (scrollView.contentOffset.x == WIDTH * 2 ) {
            _segmentedControl.selectedSegmentIndex = 2;
        }
    }
   
}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    
//    if (scrollView.contentOffset.x <= WIDTH / 2) {
//        _segmentedControl.selectedSegmentIndex = 0;
//    } else if (scrollView.contentOffset.x > WIDTH / 2 && scrollView.contentOffset.x <= WIDTH *1.5 ) {
//        _segmentedControl.selectedSegmentIndex = 1;
//    }else if (scrollView.contentOffset.x > WIDTH *1.5  ) {
//        _segmentedControl.selectedSegmentIndex = 2;
//    }
//    NSLog(@"value = %ld",_segmentedControl.selectedSegmentIndex);
//}

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentControl
{
    
    switch (segmentControl.selectedSegmentIndex) {
        case 0:
            [_bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            break;
            
        case 1:
            [_bgScrollView setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
            break;
            
        case 2:
            [_bgScrollView setContentOffset:CGPointMake(WIDTH * 2, 0) animated:YES];
            break;
            
        default:
            break;
    }
    
}



#pragma UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _leftTableView) {
//        return _leftArray.count;
        return 20;
    } else if(tableView == _middleTableView){
//        return _middleArray.count;
        return 30;
    }else if(tableView == _rightTableView){
//        return _rightArray.count;
        return 22;

    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _leftTableView) {
        LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftMessageID" forIndexPath:indexPath];
        return cell;
    } else if(tableView == _middleTableView){
        MiddleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"middleMessageID" forIndexPath:indexPath];
        return cell;
    }else{
        RightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightMessageID" forIndexPath:indexPath];
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
